package com.cashmate.struts;

import com.cashmate.hibernate.dao.ReputationDAO;
import com.cashmate.hibernate.pojo.Employees;
import com.cashmate.hibernate.pojo.Reputation;
import com.cashmate.hibernate.pojo.Usertypes;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

public class EditReputation extends ActionSupport {

    private static Logger logger = Logger.getLogger(EditReputation.class);
    private String oper;
    private String id;
    private String type;
    private ReputationDAO rpdao = (ReputationDAO) ServletActionContext.getServletContext().getAttribute("reputationDAO");
    private Employees emps = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");
    private Usertypes ut = (Usertypes) ServletActionContext.getRequest().getSession().getAttribute("usertype");

    @Action(value = "/edit-reputation", results = {
        @Result(name = "duplicate", location = "../error/formError.jsp"),
        @Result(name = "error", location = "../error/messages.jsp")
    })
    @Override
    public String execute() {
        try {
            Reputation r = null;
            if (oper.equals("add")) {
                if (rpdao.hasDuplicate(type)) {
                    addActionError("ERROR : Reputation " + type + " already exists.");
                    return "duplicate";
                }
                r = new Reputation(type, true);
                rpdao.insert(r);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") added a new Reputation: {repid:" + id + ", type:" + type + "}");
            } else if (oper.equals("edit")) {
                r = rpdao.get(Integer.parseInt(id));
                if (!r.getType().equals(type)) {
                    if (rpdao.hasDuplicate(type)) {
                        addActionError("ERROR : Reputation " + type + " already exists.");
                        return "duplicate";
                    }
                }
                String old = r.getType();
                r.setType(type);
                rpdao.update(r);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Reputation(" + old + ") with new values: {type:" + type + "}");
            } else if (oper.equals("del")) {
                r = rpdao.get(Integer.parseInt(id));
                r.setIsactive(false);
                rpdao.update(r);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") deleted Reputation(" + r.getType() + ")");
            }
        } catch (Exception e) {
            if (emps != null) {
                logger.fatal("(" + emps.getIdnumber() + "):" + e.getLocalizedMessage(), e);
            } else {
                logger.fatal(e.getLocalizedMessage(), e);
            }
            addActionError(e.getLocalizedMessage());
            return ERROR;
        }
        return NONE;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setOper(String oper) {
        this.oper = oper;
    }
}
